package com.myx.design.single.realization;

/**
 * 描述: 单例模式饿汉模式
 * @author: myx
 * @date: 2018/11/28
 * Copyright © 2018-hotpot. All rights reserved.
 */
public class UserSingleHungry {
    /**
     * 类初始化时,会立即加载该对象，线程天生安全,调用效率高,如果在不使用对象时候，会浪费内存,垃圾回收机制不会回收
     */
    private static final UserSingleHungry singleHungry =new UserSingleHungry();
    /**
     * 私有化构造函数是实现单利的第一步
     */
    private UserSingleHungry(){
        System.out.println("私有化构造函数");
    }
    public static UserSingleHungry getInstance(){
        System.out.println("饿汉模式获取数据");
        return singleHungry;
    }
    public static void main(String[] args) {
        //判断是否为同一对象
        System.out.println(UserSingleHungry.getInstance()==UserSingleHungry.getInstance());
    }
}
